package com.tz.springmvc.sysmanage.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用于处理文件上传的contoller
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/sysmanage/test")
public class UploadTestController {
	 
	//进入upload的页面
	@RequestMapping("/gotoUpload")
	public String gotoUpload(){
		return "sysmanage/test/uploadTest";
	}
	
	@RequestMapping("/uploadFile")
	public String uploadFile(MultipartFile uploadFile,HttpServletRequest request){
		//将我们的文件保存到项目中某个指定的文件加下面
		//得到项目的绝对路径
/*		String rootPath = "upload"
*/		String rootPath = request.getServletContext().getRealPath("upload");
		//将上传的图片写入指定的文件
		if(uploadFile!=null){
			//获取上传文件的名称
			String fileName = uploadFile.getOriginalFilename();
			String suffix = fileName.substring(fileName.lastIndexOf("."));
			//为了保险起见,我们给上传的图片重新指定一个名称
			String tempFileName = UUID.randomUUID().toString()+suffix;
			//获取上传上传的后缀
			File fileTemp = new File(rootPath);
			if(!fileTemp.exists()){
				fileTemp.mkdir();
			}
			
			File file = new File(rootPath+"\\"+tempFileName);
			try {
				//讲上传的文件写入指定路径
				uploadFile.transferTo(file);
			} catch (IllegalStateException e) {
 				e.printStackTrace();
			} catch (IOException e) {
 				e.printStackTrace();
			}
			
/*			http://127.0.0.1:8080/springmvc_05_inter_uplaod/
*/			request.setAttribute("uploadFilePath", "upload/"+tempFileName);
		}
		
		return "sysmanage/test/uploadTest";
	}
	
	
	@RequestMapping("/uploadFileBatch")
	public String uploadFileBatch(@RequestParam MultipartFile[] uploadFileBatch,HttpServletRequest request){
		//将我们的文件保存到项目中某个指定的文件加下面
		//得到项目的绝对路径
		String rootPath = request.getServletContext().getRealPath("upload");
		Map<String,Object> mapPth = new HashMap<String,Object>();
		if(uploadFileBatch!=null&&uploadFileBatch.length>0){
			for(MultipartFile uploadFile :uploadFileBatch)
				//将上传的图片写入指定的文件
				if(uploadFile!=null){
					//获取上传文件的名称
					String fileName = uploadFile.getOriginalFilename();
					String suffix = fileName.substring(fileName.lastIndexOf("."));
					//为了保险起见,我们给上传的图片重新指定一个名称
					String tempFileName = UUID.randomUUID().toString()+suffix;
					//获取上传上传的后缀
					File fileTemp = new File(rootPath);
					if(!fileTemp.exists()){
						fileTemp.mkdir();
					}
					
					File file = new File(rootPath+"\\"+tempFileName);
					try {
						//讲上传的文件写入指定路径
						uploadFile.transferTo(file);
					} catch (IllegalStateException e) {
		 				e.printStackTrace();
					} catch (IOException e) {
		 				e.printStackTrace();
					}
					mapPth.put(fileName, tempFileName);				
	 			}
			request.setAttribute("uploadMap", mapPth);
		}
		return "sysmanage/test/uploadTest";
	}
	
	
	@RequestMapping("/downloadFile")
	public void downloadFile(String fileName,
			HttpServletRequest request,HttpServletResponse response){
		if(fileName!=null){
			String realPath = request.getServletContext().getRealPath("upload/");
			File file = new File(realPath,fileName);
			OutputStream out = null;
			if(file.exists()){
				response.setContentType("application/force-download"); //设置强制下载不打开
				response.setHeader("Content-Disposition", "attachment;filename="+fileName);//设置文件名
				try {
					out = response.getOutputStream();
					out.write(FileUtils.readFileToByteArray(file));
					out.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					if(out!=null)
						try {
							out.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				}						
			}			
		}			
	}
	
	
}
